<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ attribute name="jssdkconfig" type="java.util.Map" required="false" description="微信分享验证信息"%>
<%@ attribute name="sysinfo" type="java.util.Map" required="true" description="request常用变量"%>
<%@ attribute name="debug" type="java.lang.Boolean" required="false" description="是否调试"%>
<%@ attribute name="hideOptionMenu" type="java.lang.Boolean" required="false" description="隐藏右上角菜单"%>
<%@ attribute name="showOptionMenu" type="java.lang.Boolean" required="false" description="显示右上角菜单"%>
<%@ attribute name="hideMenuItems" type="java.lang.Boolean" required="false" description="批量隐藏菜单项"%>
<%@ attribute name="showMenuItems" type="java.lang.Boolean" required="false" description="批量显示菜单项"%>
<%@ attribute name="hideAllNonBaseMenuItem" type="java.lang.Boolean" required="false" description="隐藏所有非基本菜单项"%>
<%@ attribute name="showAllNonBaseMenuItem" type="java.lang.Boolean" required="false" description="显示所有被隐藏的非基本菜单项"%>
<%@ attribute name="shareData" type="java.util.Map" required="false" description="微信分享数据"%>
<%@ attribute name="shareCallback" type="java.lang.String" required="false" description="分享成功后回调函数名"%>

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
<c:if test="${not empty sysinfo}">
window.sysinfo = {
	'uniacid': "${sysinfo['uniacid']}",
	'openid': "${sysinfo['openid']}",
	'uuid': "${sysinfo['uuid']}",
	'uid': "${sysinfo['uid']}",
	'siteroot': "${sysinfo['siteroot']}",
	'siteurl': "${sysinfo['siteurl']}",
	'actionId': "${sysinfo['actionId']}",
	'attachurl': "${sysinfo['attachurl']}"
}
</c:if>
<c:if test="${not empty jssdkconfig}">
	// jssdk config 对象
	var jssdkconfig=[];
	jssdkconfig.signature="${jssdkconfig['signature']}";
	jssdkconfig.nonceStr="${jssdkconfig['nonceStr']}";
	jssdkconfig.appId="${jssdkconfig['appId']}";
	jssdkconfig.timestamp="${jssdkconfig['timestamp']}";
	// 是否启用调试
	<c:choose> 
	<c:when test="${not empty debug}"> 
		jssdkconfig.debug = ${debug};
	</c:when>  
	<c:otherwise> 
		jssdkconfig.debug = false;
	</c:otherwise>  
	</c:choose> 
	jssdkconfig.jsApiList = [
		'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'onMenuShareQZone',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'translateVoice',
        'startRecord',
        'stopRecord',
        'onVoiceRecordEnd',
        'playVoice',
        'onVoicePlayEnd',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard'
	];
	wx.config(jssdkconfig);
	wx.ready(function () {
	<c:if test="${hideOptionMenu}">
		wx.hideOptionMenu();
	</c:if>
	
	<c:if test="${showOptionMenu}">
		wx.showOptionMenu();
	</c:if>
	
	<c:if test="${hideMenuItems}">
		 wx.hideMenuItems({
      menuList: [
        'menuItem:readMode', // 阅读模式
        'menuItem:share:timeline', // 分享到朋友圈
        'menuItem:copyUrl' // 复制链接
      ],
      success: function (res) {
      },
      fail: function (res) {
      }
    });
	</c:if>
	
	<c:if test="${showMenuItems}">
		wx.showMenuItems({
      menuList: [
        'menuItem:readMode', // 阅读模式
        'menuItem:share:timeline', // 分享到朋友圈
        'menuItem:copyUrl' // 复制链接
      ],
      success: function (res) {
      },
      fail: function (res) {
      }
    });
	</c:if>
	
	<c:if test="${hideAllNonBaseMenuItem}">
	  wx.hideAllNonBaseMenuItem({
      	success: function () {
      }
    });
	</c:if>
	
	<c:if test="${showAllNonBaseMenuItem}">
		wx.showAllNonBaseMenuItem({
      success: function () {
      }
    });
	</c:if>
	
	<c:if test="${not empty shareData}">
	function share(shareData,res){
	$.ajax({
				type: 'post',
				url: window.sysinfo.siteroot+'/weixin/share',
				data:{shareId:window.sysinfo.uuid,uniacid:window.sysinfo.uniacid,resMsg:res.errMsg,shareImg:shareData.imgUrl,shareLink:shareData.link,shareDesc:shareData.desc,openid:window.sysinfo.openid,actionId:window.sysinfo.actionId,shareTitle:shareData.title},
				cache: false,
				dataType: 'json',
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				success:function(data){
		        },
				error:function(XMLHttpRequest, textStatus, errorThrown){
				}
			});	
	}
	shareData = {
	    'title' : "${shareData.title}",
	    'desc': "${shareData.desc}",
	    'link': "${shareData.link}",
	    'imgUrl': "${shareData.imgUrl}",
    };
    //分享朋友
    wx.onMenuShareAppMessage({
	    title: shareData.title,
	    desc: shareData.desc,
	    link: shareData.link,
	    imgUrl:shareData.imgUrl,
	    trigger: function (res) {
	    	//share(shareData,res);
	    },
	    success: function (res) {
	    	share(shareData,res);
	    	<c:if test="${not empty shareCallback}">
	    		eval('${shareCallback}()');
	    	</c:if>
	    },
	    cancel: function (res) {
			share(shareData,res);
	    },
	    fail: function (res) {
	    	share(shareData,res);
	    }
    });
    //朋友圈
    wx.onMenuShareTimeline({
	    title: shareData.title,
	    link: shareData.link,
	    imgUrl:shareData.imgUrl,
	    trigger: function (res) {
	    },
	    success: function (res) {
	   		share(shareData,res);
	   		<c:if test="${not empty shareCallback}">
	    		eval('${shareCallback}()');
	    	</c:if>
	    },
	    cancel: function (res) {
	    share(shareData,res);
	    },
	    fail: function (res) {
	    share(shareData,res);
	    }
	});
	</c:if>
});
</c:if>
</script>
